package com.bana.fvs.protocol;

import com.bana.fvs.code.*;
import com.bana.fvs.model.Controller;

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;

/**
 * 查询控制器参数
 * 雾灯报警 联动开关 雷达
 * 雾灯亮度等6个参数
 */
public class QueryControllerConfig {

    public int getId()
    {
        return 0x0b;
    }

    public byte[] toBuffer(int kzqbsm)
    {
        byte[] buffer=new byte[6];
        buffer[0]=(byte)0x81;
        buffer[1]=(byte)kzqbsm;
        buffer[2]=(byte)getId();
        buffer[3]=(byte)0x00;
        buffer[4]=(byte)0x00;
        buffer[5]=(byte)0xff;
        return buffer;
    }

    public Controller toModel(byte[] buffer) throws IOException {
        ByteArrayInputStream bais = new ByteArrayInputStream(buffer);
        DataInputStream dis = new DataInputStream(bais);
        dis.skipBytes(1);
        Controller controller=new Controller();
        controller.setKzqbsm(dis.readByte());
        //地址
        dis.skipBytes(3);

        short s6=dis.readByte();
        controller.setWdbj((s6&0x01)== 1?LampWarningStatus.Close:LampWarningStatus.Open);
        controller.setKzqldkg((s6&0x10)== LampSwitchStatus.Close?LampSwitchStatus.Close:LampSwitchStatus.Open);
        short s7=dis.readByte();
        controller.setSfsyld((s7&0x01)==LampRadarStatus.Close? LampRadarStatus.Close:LampRadarStatus.Open);


        controller.setWdkz(dis.readByte());
        dis.skipBytes(1);
        controller.setWdld(dis.readByte());
        short s11=dis.readByte();
        short s12=dis.readByte();
        controller.setLdsc(s12*128+s11);
        short s13=dis.readByte();
        short s14=dis.readByte();
        controller.setMdsc(s14*128+s13);
        controller.setHdys(dis.readByte());
        controller.setMqys(dis.readByte());
        controller.setHsjsdzsl(dis.readByte());

        if((s7&0x08)>0)
        {
            controller.setKzy(ControllSource.Timer);
        }
        else if((s6&0x80)>0)
        {
            controller.setKzy(ControllSource.Visibility);
        }
        else if(controller.getWdkz()>=1&&controller.getWdkz()<=4)
        {
            controller.setKzy(ControllSource.Command);
        }
        else
        {
            controller.setKzy(ControllSource.None);
        }
        dis.close();
        bais.close();
        return controller;
    }
}
